package com.szeastroc.mapper.system;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.szeastroc.base.mapper.BaseMapper;
import com.szeastroc.bean.system.SystemResource;
import org.apache.ibatis.annotations.Update;

/**
 * Created by jie on 2018/1/6.
 */

public interface SystemResourceMapper extends BaseMapper<SystemResource>{

    @Select("SELECT * FROM t_system_resource WHERE parent_id = 0 and  status = 1 AND id IN( SELECT resource_id FROM`t_system_role_resource` WHERE role_id IN( SELECT role_id FROM `t_system_user` WHERE id = #{userId} ) )")
    public List<SystemResource> getParentResourceByUserId(@Param("userId") Integer userId);

    @Select("SELECT * FROM `t_system_resource` WHERE id IN( SELECT resource_id FROM `t_system_role_resource` WHERE role_id = #{roleId})")
    public List<SystemResource> getSystemResourceByRoleId(@Param("roleId") Integer roleId);

    @Select("SELECT * FROM `t_system_resource` WHERE parent_id = #{parentId} and status = 1")
    public List<SystemResource> getResourceByParentId(@Param("parentId") Integer parentId);

    @Select("SELECT resource_id FROM `t_system_role_resource` WHERE role_id = #{roleId} ")
    public List<Integer> getResourceIdByRoleId(@Param("roleId") Integer roleId);

    @Select("SELECT * FROM `t_system_resource` WHERE parent_id = #{parentId} AND id IN(SELECT resource_id FROM `t_system_role_resource` WHERE role_id = #{roleId})")
    public List<SystemResource> getResourceByParentIdAndUserId(@Param("parentId") Integer parentId,@Param("roleId") Integer roleId);

    @Update("update t_system_resource set status = #{status} where id = #{resourceId}")
    public void saveResourcesStatus(@Param("resourceId") Integer resourceId,@Param("status") Integer status);

    @Select("SELECT * FROM `t_system_resource` WHERE id IN(SELECT resource_id AS id FROM `t_system_role_resource` WHERE role_id IN (${ids})) and parent_id = 0")
    public List<SystemResource> getSystemResourceParentByRoleIds(@Param("ids") String ids);

    @Select("SELECT * FROM `t_system_resource` WHERE parent_id = #{id}")
    public List<SystemResource> getSystemResourceByParentId(@Param("id") Integer id);
}
